'use strict';
const Service = require('egg').Service;
const puppeteer = require('puppeteer');
const axios = require('axios');

const LeanCloudAppId = 'XCMCokaHpqPq0WGWtGesMGty-gzGzoHsz';
const LeanCloudAppKey = 'ieRvsksRD7gJtx8VO7d5yu2N';

class CrawlerService extends Service {
  async spider() {
    this.initAxios();
    const movies = await this.fetchMovies();
    if (movies === null || movies === undefined || movies.length <= 0) {
      return;
    }
    const browser = await puppeteer.launch({ headless: false, args: [ '--no-sandbox'] });
    const page = await browser.newPage();
    await page.setViewport({ width: 1200, height: 5000 });
    console.log(`--------------${movies.length}个电影-------------`);
    for (let item of movies) {
      let data = {};
      const res2 = await this.fetchYouku(page, item['youku_url']);
      const res1 = await this.fetchTX(page, item['tengxun_url']);
      const res3 = await this.fetchAiqiyi(page, item['aiqiyi_url']);
      data = { ...res1, ...res2, ...res3, name: item.name };
      if (Object.keys(data).length <= 0) {
        return;
      }
      console.log('汇报数据', data);
      await this.report(data);
    }
    console.log('爬取结束,关闭浏览器');
    // 关闭
    await browser.close();
  }

  initAxios() {
    axios.defaults.headers['X-LC-Id'] = LeanCloudAppId;
    axios.defaults.headers['X-LC-Key'] = LeanCloudAppKey;
    axios.defaults.baseURL = 'https://vco6dgkx.lc-cn-n1-shared.com/1.1';
  }

  //获取全部电影
  async fetchMovies() {
    try {
      const res = await axios.get('/classes/Movies');
      return res.data.results;
    } catch (e) {
      return [];
    }
  }

  async report(data) {
    try {
      const res = await axios.post('/classes/Reports', data);
      console.log(res.data);
    } catch (e) {
      return [];
    }
  }

  //获取腾讯的数据
  async fetchTX(page, url) {
    try {
      if (!url) {
        return;
      }
      console.log(`腾讯的:${url}`);
      await page.goto(url, { waitUntil: 'networkidle0' ,timeout: 50000});
      const pageTitle = await page.title();
      console.log(pageTitle);
      //腾讯播放次数
      const txPlayNum = await page.$eval('#_pic_title_list_ul .figure_num .num', el => el.innerText);
      console.log('腾讯播放次数', txPlayNum);
      //腾讯评分
      const units = await page.$eval('.video_score .units', el => el.innerText);
      const decimal = await page.$eval('.video_score .decimal', el => el.innerText);
      const txScore = `${units}${decimal}`;
      console.log('腾讯评分', txScore);
      //腾讯评论数
      // for (let i = 0; i < 2; i++) {
      //   await scrollPageToBottom(page, 500, 500);
      //   await page.waitFor(1000);
      // }
      // await page.waitForSelector("#commentIframe");
      // const iframeHandle = await page.$('#commentIframe');
      // const iframe = await iframeHandle.contentFrame();
      // await iframe.waitForSelector('#J_CommentTotal');
      // const txCommentNum = await iframe.$eval('#J_CommentTotal',el=>el.innerText);
      // await page.waitForSelector('.txp_comment_hot_num');
      let txCommentNum = await page.$eval('.txp_comment_hot > .txp_btn_text', el => el.getAttribute('title'));
      txCommentNum = txCommentNum.replace('热评', '');
      console.log('腾讯评论数', txCommentNum);
      return {
        'tx_play_num': txPlayNum,
        'tx_score': txScore,
        'tx_comment_num': txCommentNum,
      };
    } catch (e) {
      console.log(e);
      return {};
    }
  }

  async fetchYouku(page, url) {
    try {
      if (!url) {
        return;
      }
      console.log(`优酷的:${url}`);
      await page.goto(url, { waitUntil: 'networkidle0',timeout: 50000 });
      const pageTitle = await page.title();
      console.log(pageTitle);
      let youkuHotNum = '';
      try {
        await page.waitForSelector(".left-title-content-wrap");
        youkuHotNum = await page.$eval('.video-heat-text', el => el.innerText);
        youkuHotNum = youkuHotNum.replace('热度', '');
        console.log('优酷热度', youkuHotNum);
      }catch (e) {
        youkuHotNum = '';
        console.log("找不到优酷热度");
      }
      let youkuScore = '';
      try{
        youkuScore = await page.$eval('.movie-score-text', el => el.innerText);
        console.log('优酷评分', youkuScore);
      }catch (e) {
        youkuScore = '';
        console.log("找不到优酷评分");
      }

      let youkuCommentsNum = '';
      try{
        let commentMenuItem = await page.waitForSelector('#youkubohMenu > a:last-child');
        await commentMenuItem.click();
        await page.waitFor(4000);
        let youkuCommentsNum = await page.$eval('.comment-list-all .comment-list-title', el => el.innerText);
        youkuCommentsNum = youkuCommentsNum.replace(/[^0-9]/ig, '');
        console.log('优酷评论数', youkuCommentsNum);
      }catch (e) {
        youkuCommentsNum = '';
        console.log("找不到优酷评论数");
      }
      return {
        'yk_hot': youkuHotNum,
        'yk_score': youkuScore,
        'yk_comment_num': youkuCommentsNum,
      };
    } catch (e) {
      console.log(e);
      return {};
    }
  }

  async fetchAiqiyi(page, url) {
    try {
      if (!url) {
        return;
      }
      console.log(`爱奇艺的:${url}`);
      await page.goto(url, { waitUntil: 'networkidle0',timeout: 50000 });
      const pageTitle = await page.title();
      console.log(pageTitle);
      await page.waitForSelector('#titleRow .qy-svgicon-hot');
      let aiqiyiHotNum = await page.$eval('#titleRow .basic-txt', el => el.innerText);
      aiqiyiHotNum = aiqiyiHotNum.replace('热度', '');
      console.log('爱奇艺热度', aiqiyiHotNum);
      await page.waitForSelector('.score-new');
      const aiqiyiScore = await page.$eval('.score-new', el => el.innerText);
      console.log('爱奇艺评分', aiqiyiScore);
      let aiqiyiCommentsNum = await page.$eval('.func-comment', el => el.innerText);
      console.log('爱奇艺评论数', aiqiyiCommentsNum);
      return {
        'aqy_hot': aiqiyiHotNum,
        'aqy_score': aiqiyiScore,
        'aqy_comment_num': aiqiyiCommentsNum,
      };
    } catch (e) {
      console.log(e);
      return {};
    }
  }
}

module.exports = CrawlerService;
